<?php

/**
 * 轻量级设计模式：通过与相似的对象共享尽可能多的内存来最小换内存或者计算开销。
 * Class Flyweight
 */

//茶
class KarakTea
{

}

// 制茶
class TeaMaker
{
    protected $availableTea = [];

    public function make($perference)
    {
        if (empty($this->availableTea[$perference])) {
            $this->availableTea[$perference] = new KarakTea();
        }
        return $this->availableTea[$perference];
    }
}

// 茶摊
class TeaShop
{
    protected $orders;
    protected $teaMaker;

    public function __construct(TeaMaker $teaMaker)
    {
        $this->teaMaker = $teaMaker;
    }

    public function takeOrder(string $teaType, int $table)
    {
        $this->orders[$table] = $this->teaMaker->make($teaType);
    }

    public function serve()
    {
        foreach ($this->orders as $table => $tea) {
            echo "Serving tea to table#" . $table;
        }
    }
}

// usage
$teaMaker = new TeaMaker();
$shop = new TeaShop($teaMaker);

$shop->takeOrder('less sugar', 1);
$shop->takeOrder('more milk', 2);
$shop->takeOrder('without sugar', 3);

$shop->serve();